﻿<%@ WebHandler Language="C#" Class="list" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.SessionState;
using System.Web.Services;

public class list : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    private StringBuilder sb = null;
    public DataSet ds = null;
    public DataTable dt = null;
    private string sqlwhere = "";
    public void ProcessRequest (HttpContext context) {
        string type = PageBase.ext_string(context.Request.Params["type"]);
        switch (type)
        {
            //保存订单
            case "order_save":
                order_save(context);
                break;
            //获取商品列表
            case "getproductlist":
                getproduct_list(context);
                break;
            //获取出售订单列表
            case "getlist":
                getinfo_list(context);
                break;
            //获取待出售订单列表
            case "getforsalelist":
                getforsalelist(context);
                break;
            //获取请求订单列表
            case "getrequestlist_mx":
                getrequestlist_mx(context);
                break;
            //获取抢单订单列表
            case "getsnatchlist_mx":
                getsnatchlist_mx(context);
                break;
            //删除
            case "delete":
                delete(context);
                break;
            //删除我的发布
            case "delete_salelist":
                delete_salelist(context);
                break;
            //发布
            case "release":
                release(context);
                break;
            //复制订单
            case "copy":
                copy(context);
                break;
            //加入购物车
            case "addcart":
                addcart(context);
                break;
            //获取购物车数量
            case "getcartsl":
                getcartsl(context);
                break;
            //查询款号
            case "get_product":
                get_product(context);
                break;
            //查询购物车列表
            case "getcartlist":
                getcartlist(context);
                break;
            //查询结算列表
            case "getaccountlist":
                getaccountlist(context);
                break;
            //删除购物车根据id
            case "cartdelete":
                cartdelete(context);
                break;
            //删除结算根据id
            case "accountdelete":
                accountdelete(context);
                break;
            //添加至结算
            case "saveaccount":
                saveaccount(context);
                break;
            //购买
            case "buy_save":
                buy_save(context);
                break;
            //请求列表
            case "getrequestinfo_list":
                getrequestinfo_list(context);
                break;
            //抢单列表
            case "getsnatchinfo_list":
                getsnatchinfo_list(context);
                break;
            //获取快递公司列表
            case "getkdgs":
                getkdgs(context);
                break;
            //获取快递公司列表
            case "forsale":
                buy_fhtobj(context);
                break;
            //获取我的订单明细
            case "getsalelist_mx":
                getsalelist_mx(context);
                break;
            //获取出售订单明细
            case "getbuylist_mx":
                getbuylist_mx(context);
                break;

            default:
                //getinfo(context);
                break;
        }
    }
    //添加发货信息后进入伯俊
    public void buy_fhtobj(HttpContext context)
    {
        string msg = "";
        bool flag=false;

        GetDate list = new GetDate();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
        {
            string s = sr.ReadToEnd();
            list = new JavaScriptSerializer().Deserialize<GetDate>(s);
        }
        if (list.ArrayObj_main.Count > 0)
        {
            string id = Utility.Trim(list.ArrayObj_main[0].ID);
            string loginid = Utility.Trim(list.ArrayObj_main[0].LOGINID);
            string username = Utility.Trim(list.ArrayObj_main[0].USERNAME);
            string sel_kdgs = Utility.Trim(list.ArrayObj_main[0].SEL_KDGS);
            string txt_kddh = Utility.Trim(list.ArrayObj_main[0].TXT_KDDH);
            string txt_tel = Utility.Trim(list.ArrayObj_main[0].TXT_TEL);
            string txt_name = Utility.Trim(list.ArrayObj_main[0].TXT_NAME);
            string txt_id = Utility.Trim(list.ArrayObj_main[0].TXT_ID);


            string sqltobj = "exec [dbo].[zdy_传入伯俊] '{0}','{1}','{2}','{3}','{4}'";
            sqltobj = string.Format(sqltobj, txt_id, sel_kdgs, txt_kddh, txt_tel, txt_name);
            DataTable dtmsg = DBHelper.ExecuteDataTable(sqltobj);
            if (dtmsg.Rows.Count > 0)
            {
                if (dtmsg.Rows[0]["zt"].ToString() == "成功")
                {
                    flag = true;
                    msg = dtmsg.Rows[0]["remsg"].ToString();
                }
                else if (dtmsg.Rows[0]["zt"].ToString() == "失败")
                {
                    flag = false;
                    msg = dtmsg.Rows[0]["remsg"].ToString();
                }
            }
            else
            {
                flag = false;
                msg = "失败，请联系管理员！";
            }
        }
        else
        {
            flag = false;
            msg = "失败，请联系管理员！";
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }


    //查询尺码下拉选项
    public void getkdgs(HttpContext context)
    {
        string sql = @"select id,typename from t_type where typelx='快递公司' and isactive='Y' order by xh ";
        DataTable dt = DBHelper.ExecuteDataTable(sql);

        string fsmessage = "";
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        sb.Append("{\"code\":\"" + code + "\",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"typename\":\"" + dt.Rows[i]["typename"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //获取订单明细列表
    public void getsalelist_mx(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);
        //用户编号
        string loginid = PageBase.ext_string(context.Request.Params["loginids"]);

        string docnos = PageBase.ext_string(context.Request.Params["docnos"]);

        string orderby = " createtime desc";

        string where = "";

        where = @"select COUNT(*) OVER(PARTITION BY 1) AS TOTAL,b.name,b.value,b.imageurl,
                 b.cmwm,b.nf,b.yscode,b.jj,b.price,a.docno,a.sl,a.createtime from [dbo].[t_sale_mx] a
                left join t_product b on a.tmid=b.id where a.docno='{0}'";
        where = string.Format(where,docnos);

        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());

        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string oldtpdz = dt.Rows[i]["imageurl"].ToString();
            string newtpdz = oldtpdz.Replace("Attach", "Attach2");
            string newtpdz2 = "http://g.satchi.com.cn" + newtpdz;

            sb.Append("{\"docno\":\"" + dt.Rows[i]["docno"].ToString() + "\"");
            sb.Append(",\"imageurl\":\"" + newtpdz2 + "\"");
            sb.Append(",\"name\":\"" + dt.Rows[i]["name"] + "\"");
            sb.Append(",\"value\":\"" + dt.Rows[i]["value"] + "\"");
            sb.Append(",\"yscode\":\"" + dt.Rows[i]["yscode"] + "\"");
            sb.Append(",\"cmwm\":\"" + dt.Rows[i]["cmwm"] + "\"");
            sb.Append(",\"price\":\"" + dt.Rows[i]["price"] + "\"");
            sb.Append(",\"nf\":\"" + dt.Rows[i]["nf"] + "\"");
            sb.Append(",\"jj\":\"" + dt.Rows[i]["jj"] + "\"");
            sb.Append(",\"sl\":\"" + dt.Rows[i]["sl"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //获取订单明细列表
    public void getbuylist_mx(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);
        //用户编号
        string loginid = PageBase.ext_string(context.Request.Params["loginids"]);

        string docnos = PageBase.ext_string(context.Request.Params["docnos"]);

        string orderby = " createtime desc";

        string where = "";

        where = @"   select COUNT(*) OVER(PARTITION BY 1) AS TOTAL,b.name,b.value,b.imageurl,
                 b.cmwm,b.nf,b.yscode,b.jj,b.price,a.docno,a.sl,a.createtime,
				 c.recename,recetel,receaddress,kd_docno,kd_name,kd_tel,bjmsg
				 from [dbo].[t_buy_mx] a
                left join t_product b on a.tmid=b.id
				left join [t_buy_main] c on a.docno=c.docno where a.docno='{0}'";
        where = string.Format(where,docnos);

        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());

        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string oldtpdz = dt.Rows[i]["imageurl"].ToString();
            string newtpdz = oldtpdz.Replace("Attach", "Attach2");
            string newtpdz2 = "http://g.satchi.com.cn" + newtpdz;

            sb.Append("{\"docno\":\"" + dt.Rows[i]["docno"].ToString() + "\"");
            sb.Append(",\"imageurl\":\"" + newtpdz2 + "\"");
            sb.Append(",\"name\":\"" + dt.Rows[i]["name"] + "\"");
            sb.Append(",\"value\":\"" + dt.Rows[i]["value"] + "\"");
            sb.Append(",\"yscode\":\"" + dt.Rows[i]["yscode"] + "\"");
            sb.Append(",\"cmwm\":\"" + dt.Rows[i]["cmwm"] + "\"");
            sb.Append(",\"price\":\"" + dt.Rows[i]["price"] + "\"");

            sb.Append(",\"recename\":\"" + dt.Rows[i]["recename"] + "\"");
            sb.Append(",\"recetel\":\"" + dt.Rows[i]["recetel"] + "\"");
            sb.Append(",\"receaddress\":\"" + dt.Rows[i]["receaddress"] + "\"");
            sb.Append(",\"kd_docno\":\"" + dt.Rows[i]["kd_docno"] + "\"");
            sb.Append(",\"kd_name\":\"" + dt.Rows[i]["kd_name"] + "\"");
            sb.Append(",\"kd_tel\":\"" + dt.Rows[i]["kd_tel"] + "\"");
            sb.Append(",\"bjmsg\":\"" + dt.Rows[i]["bjmsg"] + "\"");

            sb.Append(",\"nf\":\"" + dt.Rows[i]["nf"] + "\"");
            sb.Append(",\"jj\":\"" + dt.Rows[i]["jj"] + "\"");
            sb.Append(",\"sl\":\"" + dt.Rows[i]["sl"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //获取待售订单明细列表
    public void getforsalelistmx(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);
        //用户编号
        string loginid = PageBase.ext_string(context.Request.Params["loginids"]);

        string buyids = PageBase.ext_string(context.Request.Params["buyids"]);

        string orderby = " createtime desc";

        string where = "";

        where = @"select COUNT(*) OVER(PARTITION BY 1) AS TOTAL,a.id, a.docno,c.imageurl,b.sale_code,a.tmid,c.name,c.value,c.yscode
                 ,c.cmwm,c.price,a.sl,d.username salename,b.buy_code,e.username buyname,a.createtime from t_buy_mx a left join  t_buy_main b on a.docno=b.docno
                 left join t_product c on a.tmid=c.id
                 left join sys_users d on b.sale_code=d.loginid
                 left join sys_users e on b.buy_code=e.loginid
                 where b.id={0}";
        where = string.Format(where,buyids);

        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());

        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string oldtpdz = dt.Rows[i]["imageurl"].ToString();
            string newtpdz = oldtpdz.Replace("Attach", "Attach2");
            string newtpdz2 = "http://g.satchi.com.cn" + newtpdz;

            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"docno\":\"" + dt.Rows[i]["docno"] + "\"");
            sb.Append(",\"imageurl\":\"" + newtpdz2 + "\"");
            sb.Append(",\"tmid\":\"" + dt.Rows[i]["tmid"] + "\"");
            sb.Append(",\"name\":\"" + dt.Rows[i]["name"] + "\"");
            sb.Append(",\"value\":\"" + dt.Rows[i]["value"] + "\"");
            sb.Append(",\"yscode\":\"" + dt.Rows[i]["yscode"] + "\"");
            sb.Append(",\"cmwm\":\"" + dt.Rows[i]["cmwm"] + "\"");
            sb.Append(",\"price\":\"" + dt.Rows[i]["price"] + "\"");
            sb.Append(",\"sl\":\"" + dt.Rows[i]["sl"] + "\"");
            sb.Append(",\"sale_code\":\"" + dt.Rows[i]["sale_code"] + "\"");
            sb.Append(",\"buy_code\":\"" + dt.Rows[i]["buy_code"] + "\"");
            sb.Append(",\"salename\":\"" + dt.Rows[i]["salename"] + "\"");
            sb.Append(",\"buyname\":\"" + dt.Rows[i]["buyname"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //获取出售订单列表
    public void getforsalelist(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);
        //用户编号
        string loginid = PageBase.ext_string(context.Request.Params["loginids"]);

        string orderby = " createtime desc";

        string where = "";

        where = @"select a.id,a.docno,a.buy_code,c.username buyname,a.sale_code,d.username salename,f.typename,a.kd_name,a.kd_docno,a.kd_tel
,a.recename,a.recetel,a.receaddress,bjmsg,
a.status,sum(b.sl) sl,a.isactive,a.createtime,a.sendtime from
 t_buy_main a left join t_buy_mx b on a.docno=b.docno
 left join sys_users c on a.buy_code=c.loginid
 left join sys_users d on a.sale_code=d.loginid
 left join t_product e on b.tmid=e.id
 left join t_type f on a.kd_gs=f.id
where a.sale_code='{0}'";
        where = string.Format(where,loginid);

        string docno =PageBase.ext_string(context.Request.Params["docno"]);
        string customer_name =PageBase.ext_string(context.Request.Params["customer_name"]);
        string productvalue =PageBase.ext_string(context.Request.Params["productvalue"]);
        string status =PageBase.ext_string(context.Request.Params["status"]);

        //单据编号
        if (!string.IsNullOrEmpty(docno))
        {
            where += " and a.docno like N'%" + docno + "%'";
        }
        //经销商名称
        if (!string.IsNullOrEmpty(customer_name))
        {
            where += " and c.username like N'%" + customer_name + "%'";
        }
        //款号
        if (!string.IsNullOrEmpty(productvalue))
        {
            where += " and e.name like N'%" + productvalue + "%'";
        }
        //状态
        if (!string.IsNullOrEmpty(status))
        {
            where += " and a.status=" + status + "";
        }
        where += " group by a.id,a.docno,a.buy_code,a.sale_code,bjmsg,c.username,d.username,f.typename,a.kd_name,a.kd_docno,a.kd_tel,a.recename,a.recetel,a.receaddress,a.status,a.isactive,a.createtime,a.sendtime ";
        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());

        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"docno\":\"" + dt.Rows[i]["docno"] + "\"");
            sb.Append(",\"status\":\"" + dt.Rows[i]["status"] + "\"");
            sb.Append(",\"sl\":\"" + dt.Rows[i]["sl"] + "\"");
            sb.Append(",\"isactive\":\"" + dt.Rows[i]["isactive"] + "\"");
            sb.Append(",\"buyname\":\"" + dt.Rows[i]["buyname"] + "\"");
            sb.Append(",\"buy_code\":\"" + dt.Rows[i]["buy_code"] + "\"");
            sb.Append(",\"salename\":\"" + dt.Rows[i]["salename"] + "\"");
            sb.Append(",\"sale_code\":\"" + dt.Rows[i]["sale_code"] + "\"");
            sb.Append(",\"bjmsg\":\"" + dt.Rows[i]["bjmsg"] + "\"");
            sb.Append(",\"typename\":\"" + dt.Rows[i]["typename"] + "\"");
            sb.Append(",\"kd_name\":\"" + dt.Rows[i]["kd_name"] + "\"");
            sb.Append(",\"kd_docno\":\"" + dt.Rows[i]["kd_docno"] + "\"");
            sb.Append(",\"kd_tel\":\"" + dt.Rows[i]["kd_tel"] + "\"");

            sb.Append(",\"recename\":\"" + dt.Rows[i]["recename"] + "\"");
            sb.Append(",\"recetel\":\"" + dt.Rows[i]["recetel"] + "\"");
            sb.Append(",\"receaddress\":\"" + dt.Rows[i]["receaddress"] + "\"");

            sb.Append(",\"sendtime\":\"" + dt.Rows[i]["sendtime"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //购买订单
    public void buy_save(HttpContext context)
    {
        string msg = "";
        string sqlsave = "";
        string djbh = "";
        bool flag=false;
        //生成单号
        string sqldjbh = @"declare @BH INT;declare @MID varchar(50);SELECT @BH=ISNULL(MAX(RIGHT(docno,4)),0)+1 FROM 
                         t_lstable WHERE LEFT(docno,2)='LS' AND  RIGHT(LEFT(docno,8),6)=CONVERT(VARCHAR(10),GETDATE(),12);
                         set @MID='LS'+CONVERT(VARCHAR(10),GETDATE(),12)+CAST(right((10000+@BH),4) AS VARCHAR(20)) ;
                         SELECT @MID";
        DataTable dtResult = DBHelper.ExecuteDataTable(sqldjbh);
        djbh = dtResult.Rows[0][0].ToString();


        GetDate list = new GetDate();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
        {
            string s = sr.ReadToEnd();
            list = new JavaScriptSerializer().Deserialize<GetDate>(s);
        }
        if (list.ArrayObj_main.Count > 0)
        {
            string id = Utility.Trim(list.ArrayObj_main[0].ID);
            string loginid = Utility.Trim(list.ArrayObj_main[0].LOGINID);
            string username = Utility.Trim(list.ArrayObj_main[0].USERNAME);
            string txt_name = Utility.Trim(list.ArrayObj_main[0].TXT_NAME);
            string txt_tel = Utility.Trim(list.ArrayObj_main[0].TXT_TEL);
            string txt_address = Utility.Trim(list.ArrayObj_main[0].TXT_ADDRESS);


            //主体信息
            //sqlsave = @"INSERT INTO [t_buy_main](docno,buy_code,recename,recetel,receaddress, status, createtime, 
            //            updatetime, createrid, updaterid, isactive)
            //            VALUES
            //           ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}');";
            //sqlsave = string.Format(sqlsave, djbh, loginid,txt_name,txt_tel,txt_address, 0,  DateTime.Now.ToString(),
            //                       DateTime.Now.ToString(),id,id,'Y');
            //明细
            for (int i = 0; i < list.ArrayObj_mx.Count; i++)
            {
                sqlsave += @"insert into t_lstable( docno,sale_code,buy_code,tmid, sl, shrname, shrtel,shraddress) values
                    ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}');";
                string sale_code = Utility.Trim(list.ArrayObj_mx[i].SALE_CODE);
                string tmid = Utility.Trim(list.ArrayObj_mx[i].TMID);
                Int32? sl = Utility.ToInt(Utility.Trim(list.ArrayObj_mx[i].SL));
                sqlsave = string.Format(sqlsave,djbh,sale_code,loginid,tmid,sl,txt_name,txt_tel,txt_address);

                //sqlsave += @"insert into t_buy_mx( docno,sale_code,tmid, sl, createtime, isactive) values
                //('{0}','{1}','{2}','{3}','{4}','{5}');";
                //string customer_code = Utility.Trim(list.ArrayObj_mx[i].customer_code);
                //string tmid = Utility.Trim(list.ArrayObj_mx[i].TMID);
                //Int32? sl = Utility.ToInt(Utility.Trim(list.ArrayObj_mx[i].SL));
                //sqlsave = string.Format(sqlsave, djbh,customer_code,tmid,sl,DateTime.Now.ToString(),'Y');
            }
            //插入数据库条数
            int zxcount = 0;
            try
            {
                zxcount = DBHelper.ExecuteNonQuery(sqlsave);
            }
            catch (Exception e)
            {
                flag = false;
                msg = e.Message;
            }
            //计算前台传过来的数据条数
            int zqts =list.ArrayObj_mx.Count;

            if (zxcount == zqts)
            {
                string sql_order = "exec  [dbo].[zdy_生成订单] '{0}'";
                sql_order = string.Format(sql_order,djbh);
                int abc= DBHelper.ExecuteNonQuery(sql_order);

                flag = true;
                msg = "保存成功";
                //sql_zt = string.Format(sql_zt, djbh, "提交",id);
                string sql_zt = @"insert into [t_buy_mainlog](docno,cz,czrid,czrname,cztime,remark)
                                          values('{0}','{1}','{2}','{3}','{4}','{5}');";
                sql_zt = string.Format(sql_zt,djbh,"提交",loginid,username,DateTime.Now.ToString(),"");
                DBHelper.ExecuteNonQuery(sql_zt);
            }
            else
            {
                flag = false;
                msg = "保存失败，请联系管理员";
            }

        }
        else
        {
            flag = false;
            msg = "保存失败，请联系管理员";
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\",\"djbh\":\"" + djbh + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }


    //查询结算列表
    public void getaccountlist(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);
        //用户id
        string loginid = PageBase.ext_string(context.Request.Params["loginid"]);

        string orderby = " createtime desc";

        string where = @"select a.id accountid,b.id,b.imageurl,b.name,b.value,b.yscode,b.cmwm,b.nf,b.jj,b.price,a.buy_code,
                        a.sale_code,c.username,a.sl,a.createtime
                        from t_buyaccount a left join t_product b on a.tmid=b.id 
                        left join sys_users c on a.sale_code=c.loginid where a.buy_code='{0}' and 1=1 ";
        where = string.Format(where,loginid);

        string customer_name =PageBase.ext_string(context.Request.Params["customer_name"]);
        string productvalue =PageBase.ext_string(context.Request.Params["productvalue"]);
        string productname =PageBase.ext_string(context.Request.Params["productname"]);

        //经销商名称
        if (!string.IsNullOrEmpty(customer_name))
        {
            where += " and c.username like N'%" + customer_name + "%'";
        }
        //款号
        if (!string.IsNullOrEmpty(productvalue))
        {
            where += " and a.productvalue like N'%" + productvalue + "%'";
        }
        //品名
        if (!string.IsNullOrEmpty(productname))
        {
            where += " and c.productname like N'%" + productname + "%'";
        }
        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());

        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string oldtpdz = dt.Rows[i]["imageurl"].ToString();
            string newtpdz = oldtpdz.Replace("Attach", "Attach2");
            string newtpdz2 = "http://g.satchi.com.cn" + newtpdz;

            sb.Append("{\"imageurl\":\"" + newtpdz2 + "\"");
            sb.Append(",\"accountid\":\"" + dt.Rows[i]["accountid"] + "\"");
            sb.Append(",\"sale_code\":\"" + dt.Rows[i]["sale_code"] + "\"");
            sb.Append(",\"username\":\"" + dt.Rows[i]["username"] + "\"");
            sb.Append(",\"tmid\":\"" + dt.Rows[i]["id"] + "\"");
            sb.Append(",\"productname\":\"" + dt.Rows[i]["value"] + "\"");
            sb.Append(",\"productvalue\":\"" + dt.Rows[i]["name"] + "\"");
            sb.Append(",\"nf\":\"" + dt.Rows[i]["nf"] + "\"");
            sb.Append(",\"jj\":\"" + dt.Rows[i]["jj"] + "\"");
            sb.Append(",\"jg\":\"" + dt.Rows[i]["price"] + "\"");
            sb.Append(",\"color\":\"" + dt.Rows[i]["yscode"] + "\"");
            sb.Append(",\"cmwm\":\"" + dt.Rows[i]["cmwm"] + "\"");
            sb.Append(",\"sl\":\"" + dt.Rows[i]["sl"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }


    //添加至结算
    public void saveaccount(HttpContext context)
    {
        string msg = "";
        string sqlsave = "";
        bool flag=false;
        int insertcount = 0;
        //用户id
        string loginid = PageBase.ext_string(context.Request.Params["loginids"]);
        GetDate list = new GetDate();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
        {
            string s = sr.ReadToEnd();
            list = new JavaScriptSerializer().Deserialize<GetDate>(s);
        }
        string sql_del = @"delete from t_buyaccount where buy_code='{0}'";
        sql_del = string.Format(sql_del, loginid);
        DBHelper.ExecuteDataTable(sql_del);

        //明细
        for (int i = 0; i < list.ArrayObj_mx.Count; i++)
        {
            string sale_code = Utility.Trim(list.ArrayObj_mx[i].SALE_CODE);
            string tmid = Utility.Trim(list.ArrayObj_mx[i].TMID);
            Int32? sl = Utility.ToInt(Utility.Trim(list.ArrayObj_mx[i].SL));
            sqlsave= string.Format(sqlsave,loginid,sale_code,tmid,sl,DateTime.Now.ToString());

            string sql_zt = @"exec [dbo].[zdy_加入结算] '{0}','{1}','{2}','{3}'";
            sql_zt = string.Format(sql_zt, sale_code,loginid,tmid,sl);
            DataTable dtzt=  DBHelper.ExecuteDataTable(sql_zt);
            if (dtzt.Rows[0]["remsg"].ToString() =="成功")
            {
                insertcount++;
            }
        }

        //计算前台传过来的数据条数
        int zqts = list.ArrayObj_mx.Count;
        if (insertcount == zqts)
        {
            flag = true;
            msg = "保存成功";
        }
        else
        {
            flag = false;
            msg = "保存失败，请联系管理员";
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }

    //删除购物车
    public void cartdelete(HttpContext context)
    {
        string getcartid = PageBase.ext_string(context.Request.Params["getcartid"]);
        string msg = "";
        bool flag;
        string sqldelete = "delete from t_buycart where id={0}";
        sqldelete = string.Format(sqldelete, getcartid);
        int dtcount= DBHelper.ExecuteNonQuery(sqldelete);
        if (dtcount > 0)
        {
            flag = true;
            msg = "删除成功";
        }
        else
        {
            flag = false;
            msg = "删除失败";

        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //删除结算
    public void accountdelete(HttpContext context)
    {
        string getcartid = PageBase.ext_string(context.Request.Params["getaccountid"]);
        string msg = "";
        bool flag;
        string sqldelete = "delete from t_buyaccount where id={0}";
        sqldelete = string.Format(sqldelete, getcartid);
        int dtcount= DBHelper.ExecuteNonQuery(sqldelete);
        if (dtcount > 0)
        {
            flag = true;
            msg = "删除成功";
        }
        else
        {
            flag = false;
            msg = "删除失败";

        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //查询购物车列表
    public void getcartlist(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);
        //用户id
        string loginid = PageBase.ext_string(context.Request.Params["loginid"]);

        string orderby = " createtime desc";

        string where = @"select a.id cartid,b.id,b.imageurl,b.name,b.value,b.yscode,b.cmwm,b.nf,b.jj,b.price,a.buy_code,
                        a.sale_code,c.username,a.sl,a.createtime
                        from t_buycart a left join t_product b on a.tmid=b.id 
                        left join sys_users c on a.sale_code=c.loginid where a.buy_code={0} and 1=1 ";
        where = string.Format(where,loginid);

        string customer_name =PageBase.ext_string(context.Request.Params["customer_name"]);
        string productvalue =PageBase.ext_string(context.Request.Params["productvalue"]);
        string productname =PageBase.ext_string(context.Request.Params["productname"]);

        //经销商名称
        if (!string.IsNullOrEmpty(customer_name))
        {
            where += " and c.username like N'%" + customer_name + "%'";
        }
        //款号
        if (!string.IsNullOrEmpty(productvalue))
        {
            where += " and a.productvalue like N'%" + productvalue + "%'";
        }
        //品名
        if (!string.IsNullOrEmpty(productname))
        {
            where += " and c.productname like N'%" + productname + "%'";
        }
        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());

        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string oldtpdz = dt.Rows[i]["imageurl"].ToString();
            string newtpdz = oldtpdz.Replace("Attach", "Attach2");
            string newtpdz2 = "http://g.satchi.com.cn" + newtpdz;

            sb.Append("{\"imageurl\":\"" + newtpdz2 + "\"");
            sb.Append(",\"cartid\":\"" + dt.Rows[i]["cartid"] + "\"");
            sb.Append(",\"sale_code\":\"" + dt.Rows[i]["sale_code"] + "\"");
            sb.Append(",\"username\":\"" + dt.Rows[i]["username"] + "\"");
            sb.Append(",\"tmid\":\"" + dt.Rows[i]["id"] + "\"");
            sb.Append(",\"productname\":\"" + dt.Rows[i]["value"] + "\"");
            sb.Append(",\"productvalue\":\"" + dt.Rows[i]["name"] + "\"");
            sb.Append(",\"nf\":\"" + dt.Rows[i]["nf"] + "\"");
            sb.Append(",\"jj\":\"" + dt.Rows[i]["jj"] + "\"");
            sb.Append(",\"jg\":\"" + dt.Rows[i]["price"] + "\"");
            sb.Append(",\"color\":\"" + dt.Rows[i]["yscode"] + "\"");
            sb.Append(",\"cmwm\":\"" + dt.Rows[i]["cmwm"] + "\"");
            sb.Append(",\"sl\":\"" + dt.Rows[i]["sl"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //查询购物车数量
    public void getcartsl(HttpContext context)
    {
        string loginid = PageBase.ext_string(context.Request.Params["loginids"]);
        string msg = "";
        bool flag;
        string sql_zt = @"select sum(sl) sl from t_buycart where buy_code='{0}'";
        sql_zt = string.Format(sql_zt, loginid);
        DataTable dtzt=  DBHelper.ExecuteDataTable(sql_zt);
        if(dtzt.Rows.Count>0)
        {
            flag = true;
            msg = dtzt.Rows[0]["sl"].ToString();
        }
        else
        {
            flag = false;
            msg = "0";
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //加入购物车
    public void addcart(HttpContext context)
    {
        string sale_code = PageBase.ext_string(context.Request.Params["sale_code"]);
        string tmids = PageBase.ext_string(context.Request.Params["tmids"]);
        string sl = PageBase.ext_string(context.Request.Params["sl"]);
        string loginid = PageBase.ext_string(context.Request.Params["loginids"]);
        string msg = "";
        bool flag;
        string sql_zt = @"exec [dbo].[zdy_加入购物车] '{0}','{1}','{2}','{3}'";
        sql_zt = string.Format(sql_zt, sale_code,loginid,tmids,sl);
        DataTable dtzt=  DBHelper.ExecuteDataTable(sql_zt);
        if (dtzt.Rows[0]["remsg"].ToString() =="成功")
        {
            flag = true;
            msg = "成功";
        }
        else
        {
            flag = false;
            msg = "库存量不足";
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //查询款号
    public void get_product(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);

        string orderby = "nf desc";
        string where = @"select  COUNT(*) OVER(PARTITION BY 1) AS TOTAL,id,no,name,yscode,
                         ysname,cmnm,cmwm,value,nf,jj,price from [t_product] where pinpai=374 
                         and nf>='2016' and nf<>'不区分年份' and left(name,2)<>'89' and 1=1";

        string txt_tm =PageBase.ext_string(context.Request.Params["txt_tm"]);
        string txt_kh =PageBase.ext_string(context.Request.Params["txt_kh"]);
        string txt_pm =PageBase.ext_string(context.Request.Params["txt_pm"]);
        string txt_color =PageBase.ext_string(context.Request.Params["txt_color"]);
        string txt_wm =PageBase.ext_string(context.Request.Params["txt_wm"]);
        string txt_nf =PageBase.ext_string(context.Request.Params["txt_nf"]);
        string txt_jj =PageBase.ext_string(context.Request.Params["txt_jj"]);
        //条码
        if (!string.IsNullOrEmpty(txt_tm))
        {
            where += " and no like N'%" + txt_tm + "%'";
        }
        //款号
        if (!string.IsNullOrEmpty(txt_kh))
        {
            where += " and name like N'%" + txt_kh + "%'";
        }
        //品名
        if (!string.IsNullOrEmpty(txt_pm))
        {
            where += " and value like N'%" + txt_pm + "%'";
        }
        //颜色
        if (!string.IsNullOrEmpty(txt_color))
        {
            where += " and yscode=" + txt_color + "";
        }
        //外码
        if (!string.IsNullOrEmpty(txt_wm))
        {
            where += " and cmwm=" + txt_wm + "";
        }
        //年份
        if (!string.IsNullOrEmpty(txt_nf))
        {
            where += " and nf=" + txt_nf + "";
        }
        //季节
        if (!string.IsNullOrEmpty(txt_jj))
        {
            where += " and jj=" + txt_jj + "";
        }

        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());

        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"no\":\"" + dt.Rows[i]["no"] + "\"");
            sb.Append(",\"product_value\":\"" + dt.Rows[i]["name"] + "\"");
            sb.Append(",\"yscode\":\"" + dt.Rows[i]["yscode"] + "\"");
            sb.Append(",\"ysname\":\"" + dt.Rows[i]["ysname"] + "\"");
            sb.Append(",\"size_nm\":\"" + dt.Rows[i]["cmnm"] + "\"");
            sb.Append(",\"size_wm\":\"" + dt.Rows[i]["cmwm"] + "\"");
            sb.Append(",\"product_name\":\"" + dt.Rows[i]["value"] + "\"");
            sb.Append(",\"nf\":\"" + dt.Rows[i]["nf"] + "\"");
            sb.Append(",\"jj\":\"" + dt.Rows[i]["jj"] + "\"");
            sb.Append(",\"price\":\"" + dt.Rows[i]["price"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //复制订单
    public void copy(HttpContext context)
    {
        string getids = PageBase.ext_string(context.Request.Params["getids"]);
        string msg = "";
        bool flag;
        string sql_zt = @"exec [zdy_复制订单] '{0}'";
        sql_zt = string.Format(sql_zt, getids);
        DataTable dtzt=  DBHelper.ExecuteDataTable(sql_zt);
        if (dtzt.Rows[0]["remsg"].ToString() =="复制成功")
        {
            flag = true;
            msg = "复制成功";
        }
        else
        {
            flag = false;
            msg = "复制失败";
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //删除订单
    public void delete_salelist(HttpContext context)
    {
        string getids = PageBase.ext_string(context.Request.Params["getids"]);

        string msg = "";
        bool flag;
        string sqldelete = "update [t_sale_main] set isactive='N' where id in ({0})";
        sqldelete = string.Format(sqldelete, getids);
        int dtcount= DBHelper.ExecuteNonQuery(sqldelete);
        if (dtcount > 0)
        {
            flag = true;
            msg = "删除成功";
        }
        else
        {
            flag = false;
            msg = "删除失败";

        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //删除
    public void delete(HttpContext context)
    {
        string getids = PageBase.ext_string(context.Request.Params["getids"]);
        string userid = PageBase.ext_string(context.Request.Params["userid"]);
        string username = PageBase.ext_string(context.Request.Params["usernames"]);
        string lddocno = PageBase.ext_string(context.Request.Params["lddocno"]);
        string msg = "";
        bool flag;
        string sqldelete = "update t_order_main set isactive='N' where id in ({0})";
        sqldelete = string.Format(sqldelete, getids);
        int dtcount= DBHelper.ExecuteNonQuery(sqldelete);
        if (dtcount > 0)
        {
            flag = true;
            msg = "删除成功";

            string sql_zt = @"insert into t_order_mainlog(docno,cz,czrid,czrname,cztime,remark) values('{0}','{1}','{2}','{3}','{4}','{5}')";
            sql_zt = string.Format(sql_zt,lddocno,"删除",userid,username,DateTime.Now.ToString(),"");
            DBHelper.ExecuteNonQuery(sql_zt);
        }
        else
        {
            flag = false;
            msg = "删除失败";

        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //发布
    public void release(HttpContext context)
    {
        string getids = PageBase.ext_string(context.Request.Params["getids"]);
        string msg = "";
        bool flag;
        string sql_zt = @"exec [zdy_发布订单] '{0}'";
        sql_zt = string.Format(sql_zt, getids);
        DataTable dtzt=  DBHelper.ExecuteDataTable(sql_zt);
        if (dtzt.Rows[0]["remsg"].ToString() =="发布成功")
        {
            flag = true;
            msg = "发布成功";
        }
        else
        {
            flag = false;
            msg = "发布失败";
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //获取商品列表
    public void getproduct_list(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);
        //用户id
        string userid = PageBase.ext_string(context.Request.Params["userid"]);

        string orderby = " createtime desc";

        string where = "";
        where = @"select b.imageurl,a.sale_code,c.username,b.id as tmid,b.value as productname,b.name as productvalue
				,b.nf,b.jj,b.price as jg,b.yscode as color,b.ysname as colorname
				,b.cmwm,b.cmnm,a.sl,a.remark,a.createtime
				 from [t_sale_product] a left join t_product b on a.tmid=b.id
                       left join sys_users c on a.sale_code=c.loginid
				    where rtrim(ltrim(a.isactive))='Y' and 1=1 ";
        //where = string.Format(where,userid);

        string customer_name =PageBase.ext_string(context.Request.Params["customer_name"]);
        string productvalue =PageBase.ext_string(context.Request.Params["productvalue"]);
        string productname =PageBase.ext_string(context.Request.Params["productname"]);
        string ksrq =PageBase.ext_string(context.Request.Params["ksrq"]);
        string jsrq = PageBase.ext_string(context.Request.Params["jsrq"]);

        //经销商名称
        if (!string.IsNullOrEmpty(customer_name))
        {
            where += " and c.username like N'%" + customer_name + "%'";
        }
        //款号
        if (!string.IsNullOrEmpty(productvalue))
        {
            where += " and b.name like N'%" + productvalue + "%'";
        }
        //品名
        if (!string.IsNullOrEmpty(productname))
        {
            where += " and b.value like N'%" + productname + "%'";
        }
        //开始日期
        if (!string.IsNullOrEmpty(ksrq))
        {
            where += " and a.createtime > '" + ksrq + "'";
        }
        //结束日期
        if (!string.IsNullOrEmpty(jsrq))
        {
            where += " and a.createtime <  '" + jsrq + "'";
        }
        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());

        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string oldtpdz = dt.Rows[i]["imageurl"].ToString();
            string newtpdz = oldtpdz.Replace("Attach", "Attach2");
            string newtpdz2 = "http://g.satchi.com.cn" + newtpdz;

            sb.Append("{\"imageurl\":\"" + newtpdz2 + "\"");
            sb.Append(",\"sale_code\":\"" + dt.Rows[i]["sale_code"] + "\"");
            sb.Append(",\"username\":\"" + dt.Rows[i]["username"] + "\"");
            sb.Append(",\"tmid\":\"" + dt.Rows[i]["tmid"] + "\"");
            sb.Append(",\"productname\":\"" + dt.Rows[i]["productname"] + "\"");
            sb.Append(",\"productvalue\":\"" + dt.Rows[i]["productvalue"] + "\"");
            sb.Append(",\"nf\":\"" + dt.Rows[i]["nf"] + "\"");
            sb.Append(",\"jj\":\"" + dt.Rows[i]["jj"] + "\"");
            sb.Append(",\"jg\":\"" + dt.Rows[i]["jg"] + "\"");
            sb.Append(",\"color\":\"" + dt.Rows[i]["color"] + "\"");
            sb.Append(",\"colorname\":\"" + dt.Rows[i]["colorname"] + "\"");
            sb.Append(",\"cmwm\":\"" + dt.Rows[i]["cmwm"] + "\"");
            sb.Append(",\"cmnm\":\"" + dt.Rows[i]["cmnm"] + "\"");
            sb.Append(",\"sl\":\"" + dt.Rows[i]["sl"] + "\"");
            sb.Append(",\"remark\":\"" + dt.Rows[i]["remark"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //获取抢单列表
    public void getsnatchinfo_list(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);
        //用户id
        string phone = PageBase.ext_string(context.Request.Params["phones"]);


        string sql_check = @"select b.rolesname from [dbo].[sys_users] a left join [dbo].[sys_roles] b
                                on a.role=b.id where a.phone='{0}' and rtrim(ltrim(a.isactive))='Y'";
        sql_check = string.Format(sql_check,phone);
        DataTable dt_check =DBHelper.ExecuteDataTable(sql_check);


        string orderby = " createtime desc";

        string where = "";
        if (dt_check.Rows.Count > 0)
        {
            if (dt_check.Rows[0]["rolesname"].ToString() == "经销商")
            {
                where = @"select a.id,a.requestdocno,a.docno,a.buy_takeman,a.buy_taketel,a.buy_takeaddress,d.username buyusername,
f.username saleusername,a.sale_kdgs,a.sale_kddjbh,a.sale_kdlxr,a.sale_kdtel,a.status,
a.bjmsg,sum(b.sl) sl,a.createtime,k.createtime requesttime
from t_transferDeliver_main a left join t_transferDeliver_mx b on a.docno=b.docno
left join sys_users d on a.buy_mobile=d.phone
left join sys_users f on a.sale_mobile=f.phone
left join [t_transferRequest_main] k on a.requestdocno=k.docno
where a.sale_mobile='{0}' and ltrim(rtrim(a.isactive))='Y'and ltrim(rtrim(d.isactive))='Y' and ltrim(rtrim(f.isactive))='Y'";
                where = string.Format(where, phone);
            }
            else
            {
                where = @"select a.id,a.requestdocno,a.docno,a.buy_takeman,a.buy_taketel,a.buy_takeaddress,d.username buyusername,
f.username saleusername,a.sale_kdgs,a.sale_kddjbh,a.sale_kdlxr,a.sale_kdtel,
 a.status,a.bjmsg,sum(b.sl) sl,a.createtime,k.createtime requesttime
from t_transferDeliver_main a left join t_transferDeliver_mx b on a.docno=b.docno
left join sys_users d on a.buy_mobile=d.phone
left join sys_users f on a.sale_mobile=f.phone
left join [t_transferRequest_main] k on a.requestdocno=k.docno
where ltrim(rtrim(a.isactive))='Y'and ltrim(rtrim(d.isactive))='Y' and ltrim(rtrim(f.isactive))='Y' ";
            }
        }
        string docno =PageBase.ext_string(context.Request.Params["docno_snatch"]);
        string customer_name =PageBase.ext_string(context.Request.Params["customer_name_snatch"]);
        string productvalue =PageBase.ext_string(context.Request.Params["productvalue_snatch"]);
        string status =PageBase.ext_string(context.Request.Params["status_snatch"]);
        //单据编号
        if (!string.IsNullOrEmpty(docno))
        {
            where += " and a.docno like N'%" + docno + "%'";
        }
        //经销商名称
        if (!string.IsNullOrEmpty(customer_name))
        {
            where += " and f.username like N'%" + customer_name + "%'";
        }
        //款号
        if (!string.IsNullOrEmpty(productvalue))
        {
            where += " and b.tmid in(select id from [dbo].[t_product] where no like '%"+productvalue+"%')";
        }
        //状态
        if (!string.IsNullOrEmpty(status))
        {
            where += " and a.status=" + status + "";
        }
        where += "  group by a.id,a.requestdocno,a.docno,a.buy_takeman,a.buy_taketel,a.buy_takeaddress,d.username,f.username,a.sale_kdgs,a.sale_kddjbh,a.sale_kdlxr,a.sale_kdtel,a.status,a.bjmsg,a.createtime,k.createtime  ";
        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());

        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"requestdocno\":\"" + dt.Rows[i]["requestdocno"] + "\"");
            sb.Append(",\"docno\":\"" + dt.Rows[i]["docno"] + "\"");
            sb.Append(",\"buy_takeman\":\"" + dt.Rows[i]["buy_takeman"] + "\"");
            sb.Append(",\"buy_taketel\":\"" + dt.Rows[i]["buy_taketel"] + "\"");
            sb.Append(",\"buy_takeaddress\":\"" + dt.Rows[i]["buy_takeaddress"] + "\"");
            sb.Append(",\"buyusername\":\"" + dt.Rows[i]["buyusername"] + "\"");
            sb.Append(",\"saleusername\":\"" + dt.Rows[i]["saleusername"] + "\"");
            sb.Append(",\"sale_kdgs\":\"" + dt.Rows[i]["sale_kdgs"] + "\"");
            sb.Append(",\"sale_kddjbh\":\"" + dt.Rows[i]["sale_kddjbh"] + "\"");
            sb.Append(",\"sale_kdlxr\":\"" + dt.Rows[i]["sale_kdlxr"] + "\"");
            sb.Append(",\"sale_kdtel\":\"" + dt.Rows[i]["sale_kdtel"] + "\"");
            sb.Append(",\"status\":\"" + dt.Rows[i]["status"] + "\"");
            sb.Append(",\"bjmsg\":\"" + dt.Rows[i]["bjmsg"] + "\"");
            sb.Append(",\"sl\":\"" + dt.Rows[i]["sl"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"");
            sb.Append(",\"requesttime\":\"" + dt.Rows[i]["requesttime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //获取抢单明细列表
    public void getsnatchlist_mx(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);
        //用户编号
        string loginid = PageBase.ext_string(context.Request.Params["loginids"]);
        string docnos = PageBase.ext_string(context.Request.Params["docnos"]);
        string orderby = " createtime desc";
        string where = "";
        where = @"select COUNT(*) OVER(PARTITION BY 1) AS TOTAL,a.docno,b.name,b.yscode+b.ysname ys,b.cmwm,b.value,b.nf,b.jj,b.price,
 'http://g.satchi.com.cn'+replace(imageurl,'Attach','Attach2') imgurl,a.sl,a.createtime FROM
  t_transferDeliver_mx a left join t_product b on a.tmid=b.id
 where a.docno='{0}'";
        where = string.Format(where,docnos);
        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());
        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"docno\":\"" + dt.Rows[i]["docno"].ToString() + "\"");
            sb.Append(",\"name\":\"" + dt.Rows[i]["name"] + "\"");
            sb.Append(",\"ys\":\"" + dt.Rows[i]["ys"] + "\"");
            sb.Append(",\"cmwm\":\"" + dt.Rows[i]["cmwm"] + "\"");
            sb.Append(",\"value\":\"" + dt.Rows[i]["value"] + "\"");
            sb.Append(",\"nf\":\"" + dt.Rows[i]["nf"] + "\"");
            sb.Append(",\"jj\":\"" + dt.Rows[i]["jj"] + "\"");
            sb.Append(",\"price\":\"" + dt.Rows[i]["price"] + "\"");
            sb.Append(",\"imgurl\":\"" + dt.Rows[i]["imgurl"] + "\"");
            sb.Append(",\"sl\":\"" + dt.Rows[i]["sl"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //获取请求列表
    public void getrequestinfo_list(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);
        //用户id
        string phone = PageBase.ext_string(context.Request.Params["phones"]);
        string sql_check = @"select b.rolesname from [dbo].[sys_users] a left join [dbo].[sys_roles] b
                                on a.role=b.id where a.phone='{0}' and rtrim(ltrim(a.isactive))='Y'";
        sql_check = string.Format(sql_check,phone);
        DataTable dt_check =DBHelper.ExecuteDataTable(sql_check);
        string orderby = " 申请时间 desc";
        string where = "";
        if (dt_check.Rows.Count > 0)
        {
            if (dt_check.Rows[0]["rolesname"].ToString() == "经销商")
            {
                where = @"select a.docno sqdh,case  when b.docno is null then '未抢单' else '已抢单' end 是否抢单
,b.docno qddh,c.username 调拨店仓,d.username 抢单店仓
,b.sale_kdgs 快递公司,b.sale_kddjbh 快递单号,case b.status when 80 then '待发货' when '99' then '完成' else '' end 状态
,bjmsg 提示信息,ret_docno 销售退货单,sale_docno 销售单,
a.updatetime 申请时间,b.createtime 抢单时间
from [t_transferRequest_main] a
left join [t_transferDeliver_main] b on a.docno=b.requestdocno
left join sys_users c on a.mobile=c.phone
left join sys_users d on b.sale_storecode=d.loginid and b.sale_mobile=d.phone
where ltrim(rtrim(a.isactive))='Y' and a.status=1 and (a.mobile='{0}' or b.sale_MOBILE='{0}')
	";
                where = string.Format(where, phone);
            }
            else
            {
                where = @"select a.docno sqdh,case  when b.docno is null then '未抢单' else '已抢单' end 是否抢单
,b.docno qddh,c.username 调拨店仓,d.username 抢单店仓
,b.sale_kdgs 快递公司,b.sale_kddjbh 快递单号,case b.status when 80 then '待发货' when '99' then '完成' else '' end 状态
,bjmsg 提示信息,ret_docno 销售退货单,sale_docno 销售单,
a.updatetime 申请时间,b.createtime 抢单时间
from [t_transferRequest_main] a
left join [t_transferDeliver_main] b on a.docno=b.requestdocno
left join sys_users c on a.mobile=c.phone
left join sys_users d on b.sale_storecode=d.loginid and b.sale_mobile=d.phone
where ltrim(rtrim(a.isactive))='Y'";
            }
        }
        string docno =PageBase.ext_string(context.Request.Params["docno"]);
        string customer_name =PageBase.ext_string(context.Request.Params["customer_name"]);
        string productvalue =PageBase.ext_string(context.Request.Params["productvalue"]);
        string status =PageBase.ext_string(context.Request.Params["status"]);
        //单据编号
        if (!string.IsNullOrEmpty(docno))
        {
            where += " and a.docno like N'%" + docno + "%'";
        }
        //经销商名称
        if (!string.IsNullOrEmpty(customer_name))
        {
            where += " and c.username like N'%" + customer_name + "%'";
        }
        //款号
        if (!string.IsNullOrEmpty(productvalue))
        {
            // where += " and b.tmid in(select id from [dbo].[t_product] where no like '%"+productvalue+"%')";
        }
        //状态
        if (!string.IsNullOrEmpty(status))
        {
            where += " and b.status=" + status + "";
        }
        where += "  ";
        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());
        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"sqdh\":\"" + dt.Rows[i]["sqdh"].ToString() + "\"");
            sb.Append(",\"是否抢单\":\"" + dt.Rows[i]["是否抢单"] + "\"");
            sb.Append(",\"qddh\":\"" + dt.Rows[i]["qddh"] + "\"");
            sb.Append(",\"调拨店仓\":\"" + dt.Rows[i]["调拨店仓"] + "\"");
            sb.Append(",\"抢单店仓\":\"" + dt.Rows[i]["抢单店仓"] + "\"");
            sb.Append(",\"快递公司\":\"" + dt.Rows[i]["快递公司"] + "\"");
            sb.Append(",\"快递单号\":\"" + dt.Rows[i]["快递单号"] + "\"");
            sb.Append(",\"状态\":\"" + dt.Rows[i]["状态"] + "\"");
            sb.Append(",\"提示信息\":\"" + dt.Rows[i]["提示信息"] + "\"");
            sb.Append(",\"销售退货单\":\"" + dt.Rows[i]["销售退货单"] + "\"");
            sb.Append(",\"销售单\":\"" + dt.Rows[i]["销售单"] + "\"");
            sb.Append(",\"申请时间\":\"" + dt.Rows[i]["申请时间"] + "\"");
            sb.Append(",\"抢单时间\":\"" + dt.Rows[i]["抢单时间"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //获取请求明细列表
    public void getrequestlist_mx(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);
        //用户编号
        string loginid = PageBase.ext_string(context.Request.Params["loginids"]);
        string sqtype = PageBase.ext_string(context.Request.Params["sqtype"]);
        string docnos = PageBase.ext_string(context.Request.Params["docnos"]);
        string orderby = " createtime desc";
        string where = "";
        if (sqtype == "view_sq")
        {
            where = @" select COUNT(*) OVER(PARTITION BY 1) AS TOTAL,'http://g.satchi.com.cn'+replace(imageurl,'Attach','Attach2') imgurl,
b.value,b.name,b.yscode,b.ysname,b.cmwm,b.no,b.nf,b.jj,
b.price,a.docno as requestdocno ,'' as docno ,a.sl,a.createtime 
 from [dbo].t_transferRequest_mx a left join t_product b on a.tmid=b.id
 where a.docno='{0}'";
                where = string.Format(where, docnos);
        }
        else
        {
            where = @" select COUNT(*) OVER(PARTITION BY 1) AS TOTAL,
'http://g.satchi.com.cn'+replace(imageurl,'Attach','Attach2') imgurl,b.value,b.name,
b.yscode,b.ysname,b.cmwm,b.no,b.nf,b.jj,b.price,a.requestdocno,a.docno,a.sl,a.createtime
 from [dbo].[t_transferDeliver_mx] a left join t_product b on a.tmid=b.id where a.docno='{0}'";
            where = string.Format(where, docnos);
        }

        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());
        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"imgurl\":\"" + dt.Rows[i]["imgurl"].ToString() + "\"");
            sb.Append(",\"value\":\"" + dt.Rows[i]["value"] + "\"");
            sb.Append(",\"name\":\"" + dt.Rows[i]["name"] + "\"");
            sb.Append(",\"yscode\":\"" + dt.Rows[i]["yscode"] + "\"");
            sb.Append(",\"ysname\":\"" + dt.Rows[i]["ysname"] + "\"");
            sb.Append(",\"cmwm\":\"" + dt.Rows[i]["cmwm"] + "\"");
            sb.Append(",\"no\":\"" + dt.Rows[i]["no"] + "\"");
            sb.Append(",\"nf\":\"" + dt.Rows[i]["nf"] + "\"");
            sb.Append(",\"jj\":\"" + dt.Rows[i]["jj"] + "\"");
            sb.Append(",\"price\":\"" + dt.Rows[i]["price"] + "\"");
            sb.Append(",\"requestdocno\":\"" + dt.Rows[i]["requestdocno"] + "\"");
            sb.Append(",\"docno\":\"" + dt.Rows[i]["docno"] + "\"");
            sb.Append(",\"sl\":\"" + dt.Rows[i]["sl"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }

    //获取出售订单列表
    public void getinfo_list(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);
        //用户id
        string loginid = PageBase.ext_string(context.Request.Params["loginids"]);

        string orderby = " createtime desc";

        string where = "";

        where = @"select  COUNT(*) OVER(PARTITION BY 1) AS TOTAL,a.id,a.docno,a.sale_code,
a.status,sum(c.sl) sl,a.isactive,a.createtime,a.releasetime,b.username from t_sale_main a
left join sys_users b on a.sale_code=b.loginid 
left join  t_sale_mx c on a.docno=c.docno
where rtrim(ltrim(a.isactive))='Y' 
and 1=1 and a.sale_code={0}";
        where = string.Format(where,loginid);

        string docno =PageBase.ext_string(context.Request.Params["docno"]);
        string customer_name =PageBase.ext_string(context.Request.Params["customer_name"]);
        string productvalue =PageBase.ext_string(context.Request.Params["productvalue"]);
        string status =PageBase.ext_string(context.Request.Params["status"]);

        //单据编号
        if (!string.IsNullOrEmpty(docno))
        {
            where += " and a.docno like N'%" + docno + "%'";
        }
        //经销商名称
        if (!string.IsNullOrEmpty(customer_name))
        {
            where += " and b.username like N'%" + customer_name + "%'";
        }
        //款号
        if (!string.IsNullOrEmpty(productvalue))
        {
            where += " and c.productvalue like N'%" + productvalue + "%'";
        }
        //状态
        if (!string.IsNullOrEmpty(status))
        {
            where += " and a.status=" + status + "";
        }
        where += " group by a.id,a.docno,a.sale_code,a.status,a.isactive,a.createtime,a.releasetime,b.username ";
        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());

        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"docno\":\"" + dt.Rows[i]["docno"] + "\"");
            sb.Append(",\"sale_code\":\"" + dt.Rows[i]["sale_code"] + "\"");
            sb.Append(",\"status\":\"" + dt.Rows[i]["status"] + "\"");
            sb.Append(",\"sl\":\"" + dt.Rows[i]["sl"] + "\"");
            sb.Append(",\"isactive\":\"" + dt.Rows[i]["isactive"] + "\"");
            sb.Append(",\"username\":\"" + dt.Rows[i]["username"] + "\"");
            sb.Append(",\"releasetime\":\"" + dt.Rows[i]["releasetime"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }

    //新增
    public void order_save(HttpContext context)
    {
        string msg = "";
        string sqlsave = "";
        string djbh = "";
        bool flag=false;
        //生成单号
        string sqldjbh = @"declare @BH INT;declare @MID varchar(50);SELECT @BH=ISNULL(MAX(RIGHT(docno,4)),0)+1 FROM 
                         t_sale_main WHERE LEFT(docno,2)='SA' AND  RIGHT(LEFT(docno,8),6)=CONVERT(VARCHAR(10),GETDATE(),12);
                         set @MID='SA'+CONVERT(VARCHAR(10),GETDATE(),12)+CAST(right((10000+@BH),4) AS VARCHAR(20)) ;
                         SELECT @MID";
        DataTable dtResult = DBHelper.ExecuteDataTable(sqldjbh);
        djbh = dtResult.Rows[0][0].ToString();


        GetDate list = new GetDate();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
        {
            string s = sr.ReadToEnd();
            list = new JavaScriptSerializer().Deserialize<GetDate>(s);
        }
        if (list.ArrayObj_main.Count > 0)
        {
            string id = Utility.Trim(list.ArrayObj_main[0].ID);
            string loginid = Utility.Trim(list.ArrayObj_main[0].LOGINID);
            string username = Utility.Trim(list.ArrayObj_main[0].USERNAME);

            if (djbh != "" && djbh != null)
            {
                //主体信息
                sqlsave = @"INSERT INTO [t_sale_main](docno, sale_code, status, createtime, 
                            updatetime, createrid, updaterid, isactive)
                            VALUES
                           ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}');";
                sqlsave = string.Format(sqlsave, djbh, loginid, 0,  DateTime.Now.ToString(),
                                       DateTime.Now.ToString(),id,id,'Y' );
                //明细
                for (int i = 0; i < list.ArrayObj_mx.Count; i++)
                {
                    sqlsave += @"insert into t_sale_mx( docno,tmid,sl, remark, createtime, isactive) values
                    ('{0}','{1}','{2}','{3}','{4}','{5}');";

                    string tmid = Utility.Trim(list.ArrayObj_mx[i].ID);
                    Int32? sl = Utility.ToInt(Utility.Trim(list.ArrayObj_mx[i].SL));
                    string remark = Utility.Trim(list.ArrayObj_mx[i].MARK);
                    sqlsave = string.Format(sqlsave, djbh,tmid,sl,remark,DateTime.Now.ToString(),'Y');
                }
                //插入数据库条数
                int zxcount = 0;
                try
                {
                    zxcount = DBHelper.ExecuteNonQuery(sqlsave);
                }
                catch (Exception e)
                {
                    flag = false;
                    msg = e.Message;
                }
                //计算前台传过来的数据条数
                int zqts = list.ArrayObj_main.Count + list.ArrayObj_mx.Count;

                if (zxcount == zqts)
                {
                    flag = true;
                    msg = "保存成功";
                    //sql_zt = string.Format(sql_zt, djbh, "提交",id);
                    string sql_zt = @"insert into t_sale_mainlog(docno,cz,czrid,czrname,cztime,remark)
                                          values('{0}','{1}','{2}','{3}','{4}','{5}');";
                    sql_zt = string.Format(sql_zt,djbh,"保存",loginid,username,DateTime.Now.ToString(),"");
                    DBHelper.ExecuteNonQuery(sql_zt);
                }
                else
                {
                    flag = false;
                    msg = "保存失败，请联系管理员";
                }
            }
            else
            {
                flag = false;
                msg = "保存失败，请联系管理员";
            }
        }
        else
        {
            flag = false;
            msg = "保存失败，请联系管理员";
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\",\"djbh\":\"" + djbh + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }


    public class GetDate
    {
        public List<main> ArrayObj_main { get; set; }//主表信息
        public List<mx> ArrayObj_mx { get; set; }//明细信息
    }
    public class main
    {
        //用户id
        public string id;
        public string ID
        {
            get { return id; }
            set { id = value; }
        }
        //登录账户
        public string loginid;
        public string LOGINID
        {
            get { return loginid; }
            set { loginid = value; }
        }
        //用户名称
        public string username;
        public string USERNAME
        {
            get { return username; }
            set { username = value; }
        }
        //买手名称
        public string txt_name;
        public string TXT_NAME
        {
            get { return txt_name; }
            set { txt_name = value; }
        }
        //买手电话
        public string txt_tel;
        public string TXT_TEL
        {
            get { return txt_tel; }
            set { txt_tel = value; }
        }
        //买手地址
        public string txt_address;
        public string TXT_ADDRESS
        {
            get { return txt_address; }
            set { txt_address = value; }
        }
        //快递公司
        public string sel_kdgs;
        public string SEL_KDGS
        {
            get { return sel_kdgs; }
            set { sel_kdgs = value; }
        }
        //快递单号
        public string txt_kddh;
        public string TXT_KDDH
        {
            get { return txt_kddh; }
            set { txt_kddh = value; }
        }
        //快递单号
        public string txt_id;
        public string TXT_ID
        {
            get { return txt_id; }
            set { txt_id = value; }
        }
    }
    //尺码
    public class mx
    {
        //条码id
        public string id;
        public string ID
        {
            get { return id; }
            set { id = value; }
        }
        //tmid
        public string tmid;
        public string TMID
        {
            get { return tmid; }
            set { tmid = value; }
        }
        //经销商code
        public string customer_code;
        public string CUSTOMER_CODE
        {
            get { return customer_code; }
            set { customer_code = value; }
        }
        //买方code
        public string sale_code;
        public string SALE_CODE
        {
            get { return sale_code; }
            set { sale_code = value; }
        }
        //款号
        public string product_value;
        public string PRODUCT_VALUE
        {
            get { return product_value; }
            set { product_value = value; }
        }
        //品名
        public string product_name;
        public string PRODUCT_NAME
        {
            get { return product_name; }
            set { product_name = value; }
        }
        //颜色
        public string color;
        public string COLOR
        {
            get { return color; }
            set { color = value; }
        }
        //颜色名称
        public string colorname;
        public string COLORNAME
        {
            get { return colorname; }
            set { colorname = value; }
        }
        //尺码外码
        public string size_wm;
        public string SIZE_WM
        {
            get { return size_wm; }
            set { size_wm = value; }
        }
        //尺码内码
        public string size_nm;
        public string SIZE_NM
        {
            get { return size_nm; }
            set { size_nm = value; }
        }
        //价格
        public decimal? jg;
        public decimal? JG
        {
            get { return jg; }
            set { jg = value; }
        }
        //年份
        public string nf;
        public string NF
        {
            get { return nf; }
            set { nf = value; }
        }
        //季节
        public string jj;
        public string JJ
        {
            get { return jj; }
            set { jj = value; }
        }
        //数量
        public int? sl;
        public int? SL
        {
            get { return sl; }
            set { sl = value; }
        }
        //备注
        public string mark;
        public string MARK
        {
            get { return mark; }
            set { mark = value; }
        }

        public string imageurl;
        public string IMAGEURL
        {
            get { return imageurl; }
            set { imageurl = value; }
        }
        public string cartid;
        public string CARTID
        {
            get { return cartid; }
            set { cartid = value; }
        }
        public string customer_name;
        public string CUSTOMER_NAME
        {
            get { return customer_name; }
            set { customer_name = value; }
        }
        public string createtime;
        public string CREATETIME
        {
            get { return createtime; }
            set { createtime = value; }
        }
        public string productname;
        public string PRODUCTNAME
        {
            get { return productname; }
            set { productname = value; }
        }
        public string productvalue;
        public string PRODUCTVALUE
        {
            get { return productvalue; }
            set { productvalue = value; }
        }
        public string cmwm;
        public string CMWM
        {
            get { return cmwm; }
            set { cmwm = value; }
        }

    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}